# -*- coding: utf-8 -*-
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import time
import cv2
from kafka import KafkaConsumer
import sys

from detection.det_model import det_model

def showCam(server, topic, window_name):

    consumer = KafkaConsumer(
        topic,
        bootstrap_servers=[server])

    #fps = FPS().start()

    for msg in consumer:
        # print(msg)
        decoded = np.frombuffer(msg.value, np.uint8)
        print(decoded.size)
        #decoded = decoded.reshape(1920, 1080, 3)
        img = decoded.reshape((int)(decoded.size / (700 * 3)), 700, 3)

        #img = cv2.resize()

        dets = det_model.pred_img(img)
        for det in dets:
            det = list(map(int, det))
            # todo 画框
            cv2.rectangle(img, (det[0], det[1]),(det[2], det[3]), (0, 0, 255), 2)


        cv2.namedWindow(window_name, flags=cv2.WINDOW_FREERATIO)
        cv2.imshow(window_name, img)

        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
        #fps.update()

    #fps.stop()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    server = 'localhost:9092'
    topic = 'rtsp'
    window_name = 'fire'
    showCam(server, topic, window_name)

